package com.chain.BrotherExchange.ui.adapter;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.model.ExtractMoneyDetails;
import com.chain.BrotherExchange.ui.activity.ExtractMoneyDetailsActivity;
import com.chain.BrotherExchange.utils.DateUtils;

import java.util.List;

/**
 * 提币资产详情
 */
public class ExtractMoneyDetailsListAdapter extends RecyclerView.Adapter<ExtractMoneyDetailsListAdapter.ViewHolder> {

    private final Fragment fragment;
    private AdapterView.OnItemClickListener mOnItemClickListener;
    private List<ExtractMoneyDetails.DataBean.ListBean.ItemsBean> mDetailsList;

    public ExtractMoneyDetailsListAdapter(Fragment fragment){
        this.fragment = fragment;
    }

    @NonNull
    @Override
    public ExtractMoneyDetailsListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_extract_assets_details, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ExtractMoneyDetailsListAdapter.ViewHolder holder, int position) {
        ExtractMoneyDetails.DataBean.ListBean.ItemsBean bean = mDetailsList.get(position);
        holder.mCoinTypeTextView.setText(bean.getToken_name());
        holder.mGasTextView.setText(bean.getFee());
        holder.mTypeTextView.setText(bean.getStateText());
        holder.mDateTextView.setText(DateUtils.timestampToTime(bean.getCreate_time()));
        holder.mNumberTextView.setText(bean.getAmount());
        holder.mAccountNumberTextView.setText(bean.accountNumber());

        // 查看订单详情
        holder.mLookDetailsView.setOnClickListener(v ->
                ExtractMoneyDetailsActivity.launch(fragment, bean, position));

        // 取消订单
        holder.mCancelOrderView.setVisibility(bean.isCancel() ? View.VISIBLE : View.GONE);
        holder.mCancelOrderView.setOnClickListener(v ->
                mOnItemClickListener.onItemClick(null, v, position, 0));
    }

    public ExtractMoneyDetails.DataBean.ListBean.ItemsBean getItem(int position) {
        return mDetailsList.get(position);
    }

    @Override
    public int getItemCount() {
        return mDetailsList != null ? mDetailsList.size() : 0;
    }

    public void setCancelClickListener(ListView.OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setOrder(List<ExtractMoneyDetails.DataBean.ListBean.ItemsBean> result) {
        this.mDetailsList = result;
        notifyDataSetChanged();
    }

    public void addOrder(List<ExtractMoneyDetails.DataBean.ListBean.ItemsBean> source) {
        this.mDetailsList.addAll(source);
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView mGasTextView;
        private final View mLookDetailsView;
        private final View mCancelOrderView;
        private final TextView mDateTextView;
        private final TextView mTypeTextView;
        private final TextView mNumberTextView;
        private final TextView mCoinTypeTextView;
        private final TextView mAccountNumberTextView;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            mLookDetailsView = itemView.findViewById(R.id.look_details_view);
            mCancelOrderView = itemView.findViewById(R.id.cancel_order_text);

            mGasTextView = itemView.findViewById(R.id.gas_text);
            mDateTextView = itemView.findViewById(R.id.date_text);
            mTypeTextView = itemView.findViewById(R.id.state_text);
            mNumberTextView = itemView.findViewById(R.id.number_text);
            mCoinTypeTextView = itemView.findViewById(R.id.coin_type_text);
            mAccountNumberTextView = itemView.findViewById(R.id.account_number_text);
        }
    }
}
